home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / Metalworks / MetalworksHelp.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  3.4 KB  |  134 lines

  1. /*
  2.  * @(#)MetalworksHelp.java    1.5 98/08/26
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.*;
  16. import java.awt.*;
  17. import java.net.URL;
  18. import java.net.MalformedURLException;
  19. import java.io.*;
  20. import javax.swing.text.*;
  21. import javax.swing.event.*;
  22.  
  23. /*
  24.  * @version 1.5 08/26/98
  25.  * @author Steve Wilson
  26.  */
  27. public class MetalworksHelp extends JInternalFrame {
  28.   
  29.     public MetalworksHelp() {
  30.     super("Help", true, true, true, true);
  31.  
  32.            setFrameIcon( (Icon)UIManager.get("Tree.openIcon")); // PENDING(steve) need more general palce to get this icon
  33.     setBounds( 200, 25, 400, 400);
  34.     HtmlPane html = new HtmlPane();
  35.     setContentPane(html);
  36.     }
  37.  
  38. }
  39.  
  40.  
  41. class HtmlPane extends JScrollPane implements HyperlinkListener {
  42.     JEditorPane html;
  43.  
  44.     public HtmlPane() {
  45.     try {
  46.         File f = new File ("HelpFiles/toc.html");
  47.         String s = f.getAbsolutePath();
  48.         s = "file:"+s;
  49.         URL url = new URL(s);
  50.         html = new JEditorPane(s);
  51.         html.setEditable(false);
  52.         html.addHyperlinkListener(this);
  53.  
  54.         JViewport vp = getViewport();
  55.         vp.add(html);
  56.     } catch (MalformedURLException e) {
  57.         System.out.println("Malformed URL: " + e);
  58.     } catch (IOException e) {
  59.         System.out.println("IOException: " + e);
  60.     }    
  61.     }
  62.  
  63.     /**
  64.      * Notification of a change relative to a 
  65.      * hyperlink.
  66.      */
  67.     public void hyperlinkUpdate(HyperlinkEvent e) {
  68.     if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
  69.         linkActivated(e.getURL());
  70.     }
  71.     }
  72.  
  73.     /**
  74.      * Follows the reference in an
  75.      * link.  The given url is the requested reference.
  76.      * By default this calls <a href="#setPage">setPage</a>,
  77.      * and if an exception is thrown the original previous
  78.      * document is restored and a beep sounded.  If an 
  79.      * attempt was made to follow a link, but it represented
  80.      * a malformed url, this method will be called with a
  81.      * null argument.
  82.      *
  83.      * @param u the URL to follow
  84.      */
  85.     protected void linkActivated(URL u) {
  86.     Cursor c = html.getCursor();
  87.     Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
  88.     html.setCursor(waitCursor);
  89.     SwingUtilities.invokeLater(new PageLoader(u, c));
  90.     }
  91.  
  92.     /**
  93.      * temporary class that loads synchronously (although
  94.      * later than the request so that a cursor change
  95.      * can be done).
  96.      */
  97.     class PageLoader implements Runnable {
  98.     
  99.     PageLoader(URL u, Cursor c) {
  100.         url = u;
  101.         cursor = c;
  102.     }
  103.  
  104.         public void run() {
  105.         if (url == null) {
  106.         // restore the original cursor
  107.         html.setCursor(cursor);
  108.  
  109.         // PENDING(prinz) remove this hack when 
  110.         // automatic validation is activated.
  111.         Container parent = html.getParent();
  112.         parent.repaint();
  113.         } else {
  114.         Document doc = html.getDocument();
  115.         try {
  116.             html.setPage(url);
  117.         } catch (IOException ioe) {
  118.             html.setDocument(doc);
  119.             getToolkit().beep();
  120.         } finally {
  121.             // schedule the cursor to revert after
  122.             // the paint has happended.
  123.             url = null;
  124.             SwingUtilities.invokeLater(this);
  125.         }
  126.         }
  127.     }
  128.  
  129.     URL url;
  130.     Cursor cursor;
  131.     }
  132.  
  133. }
  134.